﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CBD.Conn;

namespace MJM.SPP.AccesoDatos
{
    public class MenuVecinosAD
    {
        public int Insertar(string nombre, string descripcion, int orden)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_MENU_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_chNOMMEN", nombre));
                cmd.Parameters.Add(new SqlParameter("@vMENU_chDESMEN", descripcion));
                cmd.Parameters.Add(new SqlParameter("@vMENU_inORDEN", orden));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                return 0;
            }

            return 1;
        }

        public DataTable Listar()
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_s_T_MAES_MENU_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da=new SqlDataAdapter();
            DataTable dt = new DataTable();
            try
            {
                cmd.Connection.Open();
                da.SelectCommand = cmd;
                da.Fill(dt);
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return dt;
        }

        public int InsertarSubMenu(string nombre, string descripcion,int menu,string url,int principal,int orden,string nomimg,byte[] datos)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_SUBMEN_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_P_inID", menu));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMMEN", nombre));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chDESMEN", descripcion));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chURLMEN", url));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inFLAPRI", principal));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inORDEN", orden));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMICO", nomimg));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_biICOMEN", datos));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                return 0;
            }
            return 1;
        }
    }
}
